#!/bin/sh
#
# unused_oids
#
# $PostgreSQL: pgsql/src/include/catalog/unused_oids,v 1.8 2006/07/31 01:16:38 tgl Exp $
#
#	finds blocks of manually-assignable oids that have not already been
#	claimed by post_hackers.  primarily useful for finding available
#	oids for new internal functions.  the numbers printed are inclusive
#	ranges of unused oids.
#
#	before using a large empty block, make sure you aren't about
#	to take over what was intended as expansion space for something
#	else.
#
#	run this script in src/include/catalog.
#


AWK="awk"

# Get FirstBootstrapObjectId from access/transam.h
FIRSTOBJECTID=`grep '#define[ 	]*FirstBootstrapObjectId' ./pg_magic_oid.h | $AWK '{ print $3 }'`
export FIRSTOBJECTID

# This value splits the OID range into two so that GPDB and GPSQL can add
# new built-in objects in parallel.
LOWESTGPSQLOID=`grep '#define[ 	]*LowestGPSQLBootstrapObjectId' ./pg_magic_oid.h | $AWK '{ print $3 }'`
export LOWESTGPSQLOID

# this part (down to the uniq step) should match the duplicate_oids script
# note: we exclude BKI_BOOTSTRAP relations since they are expected to have
# matching DATA lines in pg_class.h

cat pg_*.h gp_*.h toasting.h indexing.h | \
egrep -v -e '^CATALOG\(.*BKI_BOOTSTRAP' | \
sed -n	-e 's/^DATA(insert *OID *= *\([0-9][0-9]*\).*$/\1/p' \
	-e 's/^CATALOG([^,]*, *\([0-9][0-9]*\).*$/\1/p' \
	-e 's/^DECLARE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \
	-e 's/^DECLARE_UNIQUE_INDEX([^,]*, *\([0-9][0-9]*\).*$/\1/p' \
	-e 's/^DECLARE_TOAST([^,]*, *\([0-9][0-9]*\), *\([0-9][0-9]*\).*$/\1,\2/p' | \
tr ',' '\n' | \
sort -n | \
uniq | \
$AWK '
BEGIN {
	last = ENVIRON["LOWESTGPSQLOID"] - 1
}
/^[0-9]/ {
	if ($1 > last + 1) {
		if ($1 > last + 2) {
			print last + 1, "-", $1 - 1;
		} else {
			print last + 1;
		}
	}
	if ($1 >= last){
		last = $1;
	}
}
END {
	if (last + 1 != ENVIRON["FIRSTOBJECTID"]) {
		print last + 1, "-", ENVIRON["FIRSTOBJECTID"]-1;
	}
}'
